package solr;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrInputDocument;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lxb on 2016/6/30.
 */
public  class SolrUtil {
  private static final String URL="http://192.168.142.128/solr/test";
    private static HttpSolrServer server = null;
   public SolrUtil(){
       server = new HttpSolrServer(URL);
       server.setAllowCompression(true);//使用gzip压缩，减少服务器数据量交换
   }
    public void add() throws SolrServerException, IOException{
    	SolrInputDocument doc = new SolrInputDocument();
    	doc.addField("id", 3);//每个solr对象都要有一个id，而且必须是唯一的
    	doc.addField("title", "测试3");
    	doc.addField("description", "这是一个测试3");


        UpdateResponse response = server.add(doc);
        server.commit();

//    	logger.info("---------------------------->Query Time"+response.getQTime());
//		logger.info("---------------------------->Elapsed Time"+response.getElapsedTime());
//		logger.info("---------------------------->Status"+response.getStatus());
    }

    public static void main(String[] args) {
        SolrQuery query = new SolrQuery();
        String querystr = "title=测试";
        query.setQuery(querystr);
        query.setHighlight(true);//开启高亮组件
        query.addHighlightField("kn_title");//高亮字段
        query.addHighlightField("context");//高亮字段
        query.setHighlightSimplePre("<font color='red'>");//标记，高亮关键字前缀
        query.setHighlightSimplePost("</font>");//后缀
        query.setHighlightSnippets(2);//结果分片数，默认为1
        query.setHighlightFragsize(1000);//每个分片的最大长度，默认为100
    }


}

